package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.service.*;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/25 9:00
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProductService productService;
    @Autowired
    private InStockInfoService inStockInfoService;
    @Autowired
    private ProductStockService productStockService;


    @Override
    //public PageInfo<InStockVO> findInStockList(String pageNum, String pageSize, String status, String type, String inNum, String startTime, String endTime)
    public PageInfo<InStockVO> findInStockList(String pageNum, String pageSize, String status, String type, String inNum, String startTime, String endTime) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        InStockVO inStockVO = new InStockVO();
        if(!StringUtils.isEmpty(status)){
            inStockVO.setStatus(Integer.parseInt(status));
        }
        if(!StringUtils.isEmpty(type)){
            inStockVO.setType(Integer.parseInt(type));
        }
        if(!StringUtils.isEmpty(inNum)){
            inStockVO.setInNum(inNum);
        }
        if(!StringUtils.isEmpty(startTime)&&!StringUtils.isEmpty(endTime)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                inStockVO.setStartTime(simpleDateFormat.parse(startTime));
                inStockVO.setEndTime(simpleDateFormat.parse(endTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        /*Example example = new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty("status")){
            criteria.andEqualTo("status",status);
        }
        if(!StringUtils.isEmpty("type")){
            criteria.andEqualTo("type",type);
        }
        if(!StringUtils.isEmpty("inNum")){
            criteria.andEqualTo("inNum",inNum);
        }
        if(!StringUtils.isEmpty("startTime")&&!StringUtils.isEmpty("endTime")){
            criteria.andBetween("createTime",startTime,endTime);
        }
        List<InStock> inStockList = inStockMapper.selectByExample(example);*/

        //List<InStockVO> list = inStockMapper.selectInStockVOByCriteria(status,type,inNum,startTime,endTime);
        List<InStockVO> list = inStockMapper.selectInStockVOByCriteria(inStockVO);


        return new PageInfo<InStockVO>(list);
    }

    @Override
    public PageInfo<InStockItemVO> detail(Long id, String pageNum) {
        PageHelper.startPage(Integer.parseInt(pageNum),10);
        List<InStockItemVO> list = inStockMapper.detail(id);
        return new PageInfo<InStockItemVO>(list);
    }

    @Override
    public InStock findOneInStock(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        return inStock;
    }

    @Override
    public void remove(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public void back(Long id) {
        InStock inStock = new InStock();
        inStock.setId(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKeySelective(inStock);
    }

    @Override
    public void publish(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
        String inNum = inStock.getInNum();
        List<InStockInfo> list = inStockInfoService.getList(inNum);
        for (InStockInfo inStockInfo : list) {
            productStockService.add(inStockInfo);
        }

    }

    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(InStockVO inStockVO, String username) {
        if (inStockVO.getSupplierId() == null) {
            //使用未知来源
            SupplierVO supplierVO = new SupplierVO();
            supplierVO.setAddress(inStockVO.getAddress());
            supplierVO.setEmail(inStockVO.getEmail());
            supplierVO.setName(inStockVO.getName());
            supplierVO.setPhone(inStockVO.getPhone());
            supplierVO.setSort(inStockVO.getSort());
            supplierVO.setContact(inStockVO.getContact());
            Supplier supplier = supplierService.add(supplierVO);
            Long id = supplier.getId();
            inStockVO.setSupplierId(id);
        }
        List<Object> products = inStockVO.getProducts();
        String inNum = UUID.randomUUID().toString().replaceAll("-", "");
        int count = 0;
        for (Object productO : products) {
            Map<String, Integer> product = (Map<String, Integer>) productO;
            Integer productId = (Integer) product.get("productId");
            Integer productNumber = (Integer) product.get("productNumber");
            ProductVO productVO = productService.edit(Long.parseLong(productId + ""));
            String pNum = productVO.getPNum();
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setpNum(pNum);
            inStockInfo.setInNum(inNum);
            inStockInfo.setProductNumber(productNumber);
            inStockInfo.setCreateTime(new Date());
            inStockInfo.setModifiedTime(new Date());
            inStockInfoService.add(inStockInfo);
            count += productNumber;
        }
        InStock inStock = new InStock();
        BeanUtils.copyProperties(inStockVO, inStock);
        inStock.setInNum(inNum);
        inStock.setOperator(username);
        inStock.setProductNumber(count);
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setStatus(2);

        inStockMapper.insert(inStock);

    }
}
